package com.ikkong.biz.controller;

import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Maps;
import com.ikkong.core.base.BaseController;
import com.ikkong.core.dao.Blade;
import com.ikkong.core.dao.Db;
import com.ikkong.core.jfinal.ext.kit.JStrKit;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.jfinal.ext.shiro.ShiroKit;
import com.ikkong.core.toolbox.Record;
import com.ikkong.system.controller.base.AdminBaseController;
import com.ikkong.system.model.Attach;
import com.ikkong.biz.meta.intercept.CoOrganizerFlowIntercept;
import com.ikkong.biz.meta.intercept.FlowIntercept;
import com.ikkong.biz.model.Flow;
import com.ikkong.biz.model.FlowSegment;
import com.ikkong.biz.model.Teacher;
import com.ikkong.biz.service.FlowSegmentService;
import com.ikkong.biz.service.FlowService;
import com.ikkong.biz.service.impl.FlowSegmentServiceImpl;
import com.ikkong.biz.service.impl.FlowServiceImpl;
import com.jfinal.aop.Before;
import org.beetl.sql.ext.jfinal.Trans;

/**
 * Generated by Blade.
 * 2016-11-24 10:53:50
 */
public class FlowController extends AdminBaseController {
	private static String CODE = "flow";
	private static String PERFIX = "tb_flow";
	private static String LIST_SOURCE = "Flow.list";
	private static String CO_ORGANIZER_LIST_SOURCE = "Flow.co_organizer_list";
	private static String BASE_PATH = "/biz/flow/";
	
	FlowService service = new FlowServiceImpl();
	FlowSegmentService fsService = new FlowSegmentServiceImpl();
	
	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "flow.html");
	}
	public void co_organizer() {
		setAttr("code", CODE);
		render(BASE_PATH + "co_organizer.html");
	}

	public void add() {
		setAttr("code", CODE);
		render(BASE_PATH + "flow_add.html");
	}
	
	private List<Attach> getAttachs(String ids){
		if(JStrKit.isBlank(ids)){
			return Collections.EMPTY_LIST;
		}
		Map<String,Object> map = Maps.newHashMap();
		map.put("ids", ids);
		return Blade.create(Attach.class).findBy("id,name,url", " find_in_set(id,#{ids})", map);
	}

	public void next() {
		String id = getPara(0);
		Flow flow = service.findById(id);
		List<Attach> flow_attachs = this.getAttachs(flow.getAttachUrl());
		List<Record> segs = Db.init().selectList("select * from tb_flow_segment where flow_id = #{flow_id} and stat=1 ",
				Record.create().set("flow_id", id));
		for (Record record : segs) {
			record.put("attachs", this.getAttachs(record.getStr("attachUrl")));
		}
		setAttr("flow", flow);
		setAttr("flow_attachs", flow_attachs);
		setAttr("segs", segs);
		setAttr("model", JsonKit.toJson(flow));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "flow_next.html");
	}

	public void view() {
		String id = getPara(0);
		Flow flow = service.findById(id);
		List<Attach> flow_attachs = this.getAttachs(flow.getAttachUrl());
		List<Record> segs = Db.init().selectList("select * from tb_flow_segment where flow_id = #{flow_id}",
				Record.create().set("flow_id", id));
		for (Record record : segs) {
			record.put("attachs", this.getAttachs(record.getStr("attachUrl")));
		}
		setAttr("flow", flow);
		setAttr("flow_attachs", flow_attachs);
		setAttr("segs", segs);
		setAttr("model", JsonKit.toJson(flow));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "flow_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE,new FlowIntercept());
		renderJson(grid);
	}
	
	public void co_organizer_list() {
		Object grid = paginate(CO_ORGANIZER_LIST_SOURCE);
		renderJson(grid);
	}

	@Before(Trans.class)
	public void save() {
		Flow flow = mapping(PERFIX, Flow.class);
        int id = service.saveFlow(flow);
        if (id>0) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

    public void update() {
		Flow flow = mapping(PERFIX, Flow.class);
		boolean temp = service.update(flow);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
}
